package com.east.service.sale.impl;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.Customer;
import com.east.bean.User;
import com.east.dao.CustomerMapper;
import com.east.service.sale.CustomerService;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService{
	@Autowired
	CustomerMapper customerMapper;

	@Override
	public Page<Customer> findByPage(Pagenation pagenation, String key,User user) {
		Page<Customer> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		customerMapper.findAll(key,null);
		return startPage;
	}

	@Override
	public Customer findByOne(Customer customer) {
		return customerMapper.selectByPrimaryKey(customer.getcId());
	}

	@Override
	public boolean saveCustomer(Customer customer,User user) {
		Customer ct = new Customer();
		ct.setcAddTime(new Date());//添加时间
		ct.setcContact(customer.getcContact());//联系人
		ct.setcCPoltow(customer.getcCPoltow());//联系人2
		ct.setcCPoltree(customer.getcCPoltree());//联系人3
		ct.setcImportance(customer.getcImportance());//客户标注 A：非常重要，B重要，C：一般,字典选择
		ct.setcOfficePhone(customer.getcOfficePhone());//座机
		ct.setCompanyId(customer.getCompanyId());//公司外键
		ct.setcPost(customer.getcPost());//联系人职务
		ct.setcRemark(customer.getcRemark());//备注
		ct.setcTelephone(customer.getcTelephone());// 联系人手机号
		ct.setcUuid(UUID.randomUUID().toString());//
		ct.setStaffId(customer.getStaffId());
		int insert = customerMapper.insert(ct);
		if(insert>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateCustomer(Customer customer,User user) {
		Customer ct = customerMapper.selectByPrimaryKey(customer.getcId());
		ct.setcAddTime(new Date());//添加时间
		ct.setcContact(customer.getcContact());//联系人
		ct.setcCPoltow(customer.getcCPoltow());//联系人2
		ct.setcCPoltree(customer.getcCPoltree());//联系人3
		ct.setcImportance(customer.getcImportance());//客户标注 A：非常重要，B重要，C：一般,字典选择
		ct.setcOfficePhone(customer.getcOfficePhone());//座机
		ct.setCompanyId(customer.getCompanyId());//公司外键
		ct.setcPost(customer.getcPost());//联系人职务
		ct.setcRemark(customer.getcRemark());//备注
		ct.setcTelephone(customer.getcTelephone());// 联系人手机号
		ct.setcUuid(UUID.randomUUID().toString());//
		ct.setStaffId(customer.getStaffId());
		int insert = customerMapper.updateByPrimaryKey(ct);
		if(insert>0){
			return true;
		}
		
		return false;
	}

	@Override
	public boolean delCustomer(Customer customer) {
		int deleteByPrimaryKey = customerMapper.deleteByPrimaryKey(customer.getcId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}
}
